package com.cx.service.impl;

import com.cx.model.MobileCard;
import com.cx.model.ServicePackage;
import com.cx.service.CallService;
import com.cx.service.NetService;
import com.cx.service.SendService;
import jdk.nashorn.internal.codegen.CompilerConstants;

import java.util.Properties;

/**
 * @author zhangshuai
 * @version V1.0
 * @Project Java2_26
 * @Package com.cx.service.impl
 * @Description:
 * @Date 2025/5/19 19:17
 */

public class SuperPackage extends ServicePackage implements CallService, SendService, NetService {
    private int talkTime;
    private int smsCount;
    private int flow;

    public SuperPackage() {
        super(78);
        this.talkTime = 200;
        this.smsCount = 50;
        this.flow = 5;
    }


    @Override
    public String toString() {
        return "套餐" +talkTime + smsCount + flow + getPrice();
    }

    @Override
    public void call(int time, MobileCard card) {
        //
        int remindTalkTime = talkTime - card.getRealTalkTime();
        //
        if(remindTalkTime >= time){
            card.setRealTalkTime(card.getRealTalkTime()+ time);
        }else {
            int usedTime = time - remindTalkTime;
            double cost = usedTime * 0.2;
            if(card.getMoney() >= cost){
                card.setRealTalkTime(card.getRealTalkTime() + remindTalkTime);
                card.setMoney(card.getMoney()-cost);
                card.setConsumAmount(card.getConsumAmount() + cost);
            }else {
                System.out.println("余额不足，无法完成通话");
            }
        }


    }

    @Override
    public void net(int flow, MobileCard card) {
        // 获取套餐剩余的流量
        int remainingFlow = this.flow - card.getRealFlow();

        // 判断套餐内的流量是否足够
        if (remainingFlow >= flow) {
            // 如果套餐内的流量足够，则直接扣减流量
            card.setRealFlow(card.getRealFlow() + flow);
        } else {
            // 如果套餐内的流量不足，则需要从余额中扣除费用
            int usedFlow = flow - remainingFlow; // 需要从余额中扣除的流量
            double cost = usedFlow * 10; // 假设超出套餐的流量费用为10元/GB

            // 检查余额是否足够
            if (card.getMoney() >= cost) {
                // 如果余额足够，则扣除费用
                card.setRealFlow(card.getRealFlow() + remainingFlow); // 使用套餐内的剩余流量
                card.setMoney(card.getMoney() - cost); // 扣除超出套餐的费用
                card.setConsumAmount(card.getConsumAmount() + cost); // 更新消费金额
            } else {
                // 如果余额不足，则提示用户
                System.out.println("余额不足，无法完成流量使用。");
            }
        }
    }

    @Override
    public void send(int count, MobileCard card) {
        // 定义一个临时变量temp，初始值为1
        int temp = 1 ;
        // 当temp小于等于count时，执行循环
        while (temp++ <= count){
            // 如果短信数量减去实际短信数量大于等于1，则将实际短信数量加1
            if(smsCount - card.getRealSmScount() >= 1){
                card.setRealSmScount(card.getRealSmScount()+1);
            // 否则，如果卡内余额大于等于0.1，则将实际短信数量加1，消费金额加0.1，卡内余额减0.1
            } else if (card.getMoney() >= 0.1) {
                card.setRealSmScount( card.getRealSmScount() + 1);
                card.setConsumAmount(card.getConsumAmount()+0.1);
                card.setMoney(card.getMoney() - 0.1);
    }
}

    }
}